# -*- coding: utf-8 -*-

import sys, os, random
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.animation import FuncAnimation
from matplotlib.ticker import MultipleLocator, FormatStrFormatter


class MyMplCanvas(FigureCanvas):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height),facecolor='white', dpi=dpi)
        self.axes = fig.add_subplot(111)
        # We want the axes cleared every time plot() is called
        self.axes.hold(False)

        self.compute_initial_figure()

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

    def compute_initial_figure(self):
        pass


class AnimationWidget(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)

        vbox = QtWidgets.QVBoxLayout()
        self.canvas = MyMplCanvas(self, width=5, height=2, dpi=100)
        vbox.addWidget(self.canvas)

        self.on_start()
        self.POINTS = 60
        self.user = [None] * self.POINTS
        self.l_user, = self.canvas.axes.plot(range(self.POINTS), self.user)

        self.canvas.axes.set_ylim(0, 1)
        self.canvas.axes.set_xlim(0, 60)

        #ymajorLocator = MultipleLocator(0.5)  # 将y轴主刻度标签设置为0.5的倍数
        #self.canvas.axes.yaxis.set_major_locator(ymajorLocator)



        self.setLayout(vbox)



    def update_line(self, i):
        temp = np.random.rand(1)
        self.user = self.user[1:] + [temp]
        self.l_user.set_ydata(self.user)
        return self.l_user,

    def on_start(self):
        self.ani = FuncAnimation(self.canvas.figure, self.update_line,
                                 blit=True, interval=1000)


if __name__ == "__main__":
    qApp = QApplication(sys.argv)
    aw = AnimationWidget()
    aw.show()
    sys.exit(qApp.exec_())